import re
def load_data(filePath):
    with open(filePath,encoding="utf-8") as f:
        content = f.read()
    return content

if __name__ == "__main__":
    content = load_data("./one.txt")
    content = content.lower()
    content = content.replace('\n','')
    content = content.replace(' ', '')
    result = re.findall(r'[a-z]',content)
    # print(content[:400])
    # print(result[:400])
    # print(len(result))

    #测试方法，计算类表中某个元素的数量
    # print(result.count('a'))
    # 字典结构，用来存放统计数据
    graph = {}
    # 不用一个个的写，将列表元素去重，然后还可以排序
    # for i in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p']
    element = list(set(result))
    print(element)
    # \w忘记还匹配了数字,上面改为[a-z]
    print(len(element))
    # 排序有些问题，但也没必要
    # element = element.sort(reverse=0)
    sort_list = []
    for i in element:
        graph[i] = result.count(i)
        sort_list.append(graph[i])
    print(graph)

    # 把字典中的值放在一个列表中，排序，然后对比把键加入
    print(sort_list)
    # 排序后,大到小
    print(sorted(sort_list,reverse=True))
    newSort_list = sorted(sort_list,reverse=True)
    # 字典插入的数据没有规律
    newestSortList = []
    for j in newSort_list:
        for i in element:
            if graph[i] == j:
                newestSortList.append(i)
    # 字典存放没有顺序，这样排序后对比了一下是对的。
    print(newestSortList)

    # 画图
    import matplotlib.pyplot as plt

    plt.bar(newestSortList,newSort_list)
    plt.show()

    plt.pie(newSort_list, labels=newestSortList, autopct='%3.2f%%')
    plt.show()

